package com.eightbears.beboy.base.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.eightbears.beboy.entity.ClassifyEntity;
import com.eightbears.beboy.entity.LocationEntity;
import com.eightbears.beboy.entity.StaticDataEntity;
import com.eightbears.beboy.entity.UserEntity;
import com.eightbears.beboy.entity.WeiChatEntity;
import com.eightbears.greendao.District;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;
import java.util.Map;

import static com.eightbears.beboy.base.util.CommonAPI.CACHE_ACCESSTOKEN;
import static com.eightbears.beboy.base.util.CommonAPI.CACHE_APP_FILE_PATH;
import static com.eightbears.beboy.base.util.CommonAPI.CACHE_FIRST_OPEN;
import static com.eightbears.beboy.base.util.CommonAPI.CACHE_STATUS_DATA;
import static com.eightbears.beboy.base.util.CommonAPI.CACHE_USER;
import static com.eightbears.beboy.base.util.CommonAPI.CACHE_USER_LOCALCATION;
import static com.eightbears.beboy.base.util.CommonAPI.CACHE_USER_SELECT_CLASSIFY;
import static com.eightbears.beboy.base.util.CommonAPI.CACHE_WCHAT;


/**
 * Created by baoga on 2017/7/2.
 */
public class SPUtil {

    private static SharedPreferences sharedPreferences;
    private static SPUtil spUtil;

    public static final String FILE_NAME = "shared_data";
    private static SharedPreferences.Editor editor;

    public static synchronized void initSharedPreference(Context context) {
        if (spUtil == null) {
            spUtil = new SPUtil(context.getApplicationContext());
        }
    }

    public SPUtil(Context context) {
        sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    /**
     * 静态数据
     */
    public static void setAlreadyLogin() {
        put(CACHE_FIRST_OPEN, false);
    }

    public static Boolean getAlreadyLogin() {
        return (Boolean) get(CACHE_FIRST_OPEN, true);
    }


    /**
     * 保存用户数据到本地
     */
    public static void saveUserInfo2Local(UserEntity userEntity) {
        if (null != userEntity) {
            MobclickAgent.onProfileSignIn(userEntity.getData().getMobile());
            putAccessToken(userEntity.getData().getAccess_token());
            putUser(userEntity);
        }
    }

    /**
     * 保存微信信息
     */
    public static void putWChat(WeiChatEntity wChatEntity) {
        put(CACHE_WCHAT, wChatEntity);
    }

    public static WeiChatEntity getWChat() {
        return (WeiChatEntity) get(CACHE_WCHAT, null);
    }

    /**
     * 登陆用户信息
     */
    public static void putUser(UserEntity user) {
        put(CACHE_USER, user);
    }

    public static UserEntity getUser() {
        return (UserEntity) get(CACHE_USER, null);
    }


    /**
     * 静态数据
     */
    public static void putAppFilePath(String filePath) {
        put(CACHE_APP_FILE_PATH, filePath);
    }

    public static String getAppFilePath() {
        return (String) get(CACHE_APP_FILE_PATH, "");
    }

    /**
     * 静态数据
     */
    public static void putAccessToken(String accesstoken) {
        put(CACHE_ACCESSTOKEN, accesstoken);

    }

    public static String getAccessToken() {
        return (String) get(CACHE_ACCESSTOKEN, "");
    }


    /**
     * 地区版本
     */
    public static void putStaticData(StaticDataEntity staticDataEntity) {
        put(CACHE_STATUS_DATA, staticDataEntity);
    }

    public static StaticDataEntity getStaticData() {
        return (StaticDataEntity) get(CACHE_STATUS_DATA, null);
    }

    /**
     * 用户位置信息
     */
    public static void putUserLocation(LocationEntity locationEntity) {
        put(CACHE_USER_LOCALCATION, locationEntity);
    }

    public static LocationEntity getUserLocation() {
        return (LocationEntity) get(CACHE_USER_LOCALCATION, null);
    }

    /**
     * 用户分类选择
     */
    public static void putSelClass(ClassifyEntity classify) {
        put(CACHE_USER_SELECT_CLASSIFY, classify);
    }

    public static ClassifyEntity getSelClass() {
        return (ClassifyEntity) get(CACHE_USER_SELECT_CLASSIFY, null);
    }

    /**
     * 用户分类选择
     */
    public static void putSelOrderClass(ClassifyEntity classify) {
        put(CACHE_USER_SELECT_CLASSIFY, classify);
    }

    public static ClassifyEntity getSelOrderClass() {
        return (ClassifyEntity) get(CACHE_USER_SELECT_CLASSIFY, null);
    }

    /**
     * 商家列表分类排序
     */
    public static void putFindSelClass(ClassifyEntity classify) {
        put("findSelClass", classify);
    }

    public static ClassifyEntity getFindSelClass() {
        return (ClassifyEntity) get("findSelClass", null);
    }

    /**
     * 只能排序按钮
     */
    public static void putAutoSortClass(ClassifyEntity classify) {
        put("AutoSortClass", classify);
    }

    public static ClassifyEntity getAutoSortClass() {
        return (ClassifyEntity) get("AutoSortClass", null);
    }

    /**
     * 只能排序按钮
     */
    public static void putLocationMenuClass(ClassifyEntity classify) {
        put("locationMenu", classify);
    }

    public static ClassifyEntity getLocationMenuClass() {
        return (ClassifyEntity) get("locationMenu", null);
    }

    /**
     * 记录发现筛选距离
     */
    public static void putLocationContentClass(ClassifyEntity classify) {
        put("locationContent", classify);
    }

    public static ClassifyEntity getLocationContentClass() {
        return (ClassifyEntity) get("locationContent", null);
    }

    /**
     * 记录发现筛选距离
     */
    public static void putFindDistrictClass(District district) {
        put("FindDistrict", district);
    }

    public static District getFindDistrictClass() {
        return (District) get("FindDistrict", null);
    }


    public static void put(String key, Object object) {

        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            try {
                String obj = CommonUtils.obj2Str(object);
                editor.putString(key, obj);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        editor.apply();
    }

    public static Object get(String key, Object defaultObject) {
        if (defaultObject instanceof String) {
            return sharedPreferences.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sharedPreferences.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sharedPreferences.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sharedPreferences.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sharedPreferences.getLong(key, (Long) defaultObject);
        } else {
            try {
                String obj = sharedPreferences.getString(key, "");
                return CommonUtils.str2Obj(obj);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sharedPreferences.getString(key, null);
        }

    }

    public static void remove(String key) {
        editor.remove(key);
        editor.apply();
    }

    public static void clear() {
        editor.clear();
        editor.apply();
    }

    public static void loginOut() {
        remove(CACHE_USER);
        remove(CACHE_ACCESSTOKEN);
        remove(CACHE_WCHAT);
    }

    public static boolean contains(String key) {
        return sharedPreferences.contains(key);
    }

    public static Map<String, ?> getAll() {
        return sharedPreferences.getAll();
    }
}
